<template>
	<view v-show="i === index">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" top="0" :down="downOption" @down="downCallback" :up="upOption"
		 @up="upCallback" @emptyclick="downCallback">
			<slot></slot>
			<np-list-one :goodsList="goods" :api="api"></np-list-one>
		</mescroll-body>
	</view>
</template>

<script>
	import base from '@/common/base.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";

	let loading = false;
	export default {
		mixins: [base, MescrollMixin, MescrollMoreItemMixin],
		props: {
			i: Number,
			index: {
				type: Number,
				default () {
					return 0
				}
			},
			tabs: {
				type: Array,
				default () {
					return []
				}
			},
			downTextWhite: {
				type: Boolean,
				default: false
			},
		},
		created() {
			if (this.i == 0 && this.downTextWhite) {
				this.downOption.textColor = '#FFFFFF';
			}
		},
		data() {
			return {
				downOption: {
					auto: false,
					textColor: '#777777',
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 空空如也 ~', // 提示
						btnText: '重新加载'
					},
				},
				goods: [],
				api: '',
			}
		},
		methods: {
			downCallback() {
				let tabItem = this.tabs[this.index];
				tabItem.min_id = 1;
				this.mescroll.resetUpScroll();
			},
			upCallback(page) {
				if (loading) {
					this.mescroll.setPageNum(page.num);
					return;
				}
				loading = true;
				let tabItem = this.tabs[this.index];
				const sentData = {};
				sentData.id = tabItem.id;
				sentData.page = page.num;
				sentData.min_id = tabItem.min_id;
				// #ifdef MP-WEIXIN||MP-QQ
				if (this.globalConfig.wechat_mod == 0) {
					sentData.sh_mode = 1;
				}
				// #endif
				this.$http.get('api/default/index-list', {
					params: sentData
				}).then(res => {
					loading = false;
					if (page.num == 1) {
						this.goods = [];
					}
					tabItem.min_id = res.min_id;
					this.api = res.api;
					let goodsList = res.list;
					goodsList.forEach(item => {
						this.goods.push(item);
					});
					let hasNext = true;
					let curPageLen = res.list.length;
					if (curPageLen < 10) {
						hasNext = false;
					}
					
					setTimeout(() => {
						this.mescroll.endSuccess(curPageLen, hasNext);
					}, 1000);

				}).catch((error) => {
					loading = false;
					this.mescroll.endErr();
				});
			},
		}
	}
</script>
